/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package transaction.committee_management;

import transaction.add_committee.*;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import template.RegistrantionPanel;
import transaction.committee_management.object.Committee;
import transaction.committee_management.object.Member;
import transaction.committee_management.object.Year;
import transaction.committee_management.object.YearCommittee;
import zsystem.SystemMessage;

/**
 *
 * @author User
 */
public class PCCommitteeManager extends RegistrantionPanel {

    /**
     * Creates new form PCAddCommittee
     */
    public PCCommitteeManager() {
        initComponents();
        initOthers();
    }

    public void resetAll() {
        initOthers();
    }

    private Object[] getYears() {
        List list = SERCommitteeManager.getYears();
        Object[] objects = list.toArray(new Object[list.size()]);
        return objects;
    }

    private Object[] getCommittees() {
        List list = SERCommitteeManager.getCommittees();
        Object[] objects = list.toArray(new Object[list.size()]);
        return objects;
    }

    private Object[] getMembers() {
        leftList = SERCommitteeManager.getMembers();
        memberList.addAll(leftList);
        Object[] objects = leftList.toArray(new Object[leftList.size()]);
        return objects;
    }

    private void getCommitteeMembers() {

        if (cboPost.getSelectedItem() != null && cboYear.getSelectedItem() != null) {

            List list = SERCommitteeManager.getCommitteeMembers(
                    ((Committee) cboPost.getSelectedItem()).getCode(),
                    ((Year) cboYear.getSelectedItem()).getCode());
            rightList.clear();
            leftList.clear();
            leftList.addAll(memberList);
            for (Iterator it = list.iterator(); it.hasNext();) {
                YearCommittee object = (YearCommittee) it.next();
                Member member = object.getMember();
                member.setStatus(object.getStatus());
                rightList.add(member);
                for (Object leftList1 : leftList) {
                    if (((Member) leftList1).getCode().equals((object.getMember()).getCode())) {
                        leftList.remove((Member) leftList1);
                        break;
                    }
                }
            }
        }
    }

    private void loadRightTable() {
        resetTable(tblRightTable);
        List list = rightList;
        DefaultTableModel tableModel = (DefaultTableModel) tblRightTable.getModel();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Member member = (Member) it.next();
            tableModel.addRow(new Object[]{member, member.getName(), member.getStatus()});
        }
    }

    private void resetTable(JTable table) {
        int rowCount = table.getRowCount();
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        for (int i = 0; i < rowCount; i++) {
            tableModel.removeRow(0);
        }
    }

    private void cboPostActionPerformed(ActionEvent e) {
        if (cboPost.getItemCount() > 0 && cboPost.getSelectedItem() != null) {
            txtNoOfMembers.setText(
                    ((Committee) cboPost.getSelectedItem())
                    .getNumberOfMembers().toString());
            getCommitteeMembers();
            loadRightTable();

            setAdded();
        }
    }

    private void cboYearActionPerformed(ActionEvent e) {
        if (cboYear.getItemCount() > 0 && cboYear.getSelectedItem() != null) {
            getCommitteeMembers();
            loadRightTable();
            setAdded();
        }
    }

    private void setAdded() {
        if (cboPost.getSelectedItem() != null) {
            int noOfMembers = ((Committee) cboPost.getSelectedItem())
                    .getNumberOfMembers();
            int noOfAdded = rightList.size();
            lblAdded.setText(noOfAdded + " / " + noOfMembers);
        }
    }

    private void setButtonMode() {
        int i = tblRightTable.getSelectedRow();
        switch (tblRightTable.getValueAt(i, 2).toString()) {
            case YearCommitteeStatus.ACTIVE:
                activeButtonMode();
                break;
            case YearCommitteeStatus.SUSPEND:
                suspendButtonMode();
                break;
        }
    }

    private void activeButtonMode() {
        btnActive.setEnabled(!true);
        btnReject.setEnabled(true);
        btnResign.setEnabled(true);
        btnSuspend.setEnabled(true);
    }

    private void suspendButtonMode() {
        btnActive.setEnabled(true);
        btnReject.setEnabled(true);
        btnResign.setEnabled(true);
        btnSuspend.setEnabled(!true);
    }

    private void defaultButtonMode() {
        btnActive.setEnabled(!true);
        btnReject.setEnabled(!true);
        btnResign.setEnabled(!true);
        btnSuspend.setEnabled(!true);
    }

    private void setStatus(String status) {
        int row = tblRightTable.getSelectedRow();
        tblRightTable.setValueAt(status, row, 2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initOthers() {
        lblAdded.setText("0 / 0");
        rightList = new ArrayList();
        leftList = new ArrayList();
        memberList = new ArrayList();
        cboPost.addActionListener(
                new java.awt.event.ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        cboPostActionPerformed(evt);
                    }
                }
        );

        cboYear.addActionListener(
                new java.awt.event.ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        cboYearActionPerformed(evt);
                    }
                }
        );
        getMembers();
        loadRightTable();
        cboPost.setSelectedItem(null);
        defaultButtonMode();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cboPost = new javax.swing.JComboBox(getCommittees());
        jLabel2 = new javax.swing.JLabel();
        txtNoOfMembers = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblRightTable = new javax.swing.JTable();
        lblAdded = new javax.swing.JLabel();
        cboYear = new javax.swing.JComboBox(getYears());
        jLabel8 = new javax.swing.JLabel();
        btnSuspend = new javax.swing.JButton();
        btnResign = new javax.swing.JButton();
        btnReject = new javax.swing.JButton();
        btnActive = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(588, 468));

        jLabel1.setText("Post :");

        jLabel2.setText("No of Members :");

        txtNoOfMembers.setEditable(false);

        tblRightTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Code", "Name", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblRightTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblRightTableMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblRightTableMousePressed(evt);
            }
        });
        tblRightTable.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tblRightTableFocusLost(evt);
            }
        });
        jScrollPane2.setViewportView(tblRightTable);
        if (tblRightTable.getColumnModel().getColumnCount() > 0) {
            tblRightTable.getColumnModel().getColumn(0).setMinWidth(20);
            tblRightTable.getColumnModel().getColumn(0).setPreferredWidth(50);
            tblRightTable.getColumnModel().getColumn(0).setMaxWidth(150);
            tblRightTable.getColumnModel().getColumn(1).setMinWidth(60);
            tblRightTable.getColumnModel().getColumn(1).setPreferredWidth(150);
            tblRightTable.getColumnModel().getColumn(1).setMaxWidth(450);
            tblRightTable.getColumnModel().getColumn(2).setMinWidth(50);
            tblRightTable.getColumnModel().getColumn(2).setPreferredWidth(50);
            tblRightTable.getColumnModel().getColumn(2).setMaxWidth(150);
        }

        lblAdded.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblAdded.setText("0/1");

        jLabel8.setText("Year :");

        btnSuspend.setText("Suspend");
        btnSuspend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuspendActionPerformed(evt);
            }
        });

        btnResign.setText("Resign");
        btnResign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResignActionPerformed(evt);
            }
        });

        btnReject.setText("Reject");
        btnReject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRejectActionPerformed(evt);
            }
        });

        btnActive.setText("Active");
        btnActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActiveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cboPost, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboYear, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 38, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtNoOfMembers, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblAdded)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnActive)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(btnReject)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnResign)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSuspend)))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboPost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNoOfMembers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAdded)
                    .addComponent(btnSuspend)
                    .addComponent(btnResign)
                    .addComponent(btnReject)
                    .addComponent(btnActive))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                .addGap(8, 8, 8))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblRightTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblRightTableMouseClicked
        setButtonMode();
    }//GEN-LAST:event_tblRightTableMouseClicked

    private void tblRightTableFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tblRightTableFocusLost
//        defaultButtonMode();
    }//GEN-LAST:event_tblRightTableFocusLost

    private void tblRightTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblRightTableMousePressed
//        setButtonMode();
    }//GEN-LAST:event_tblRightTableMousePressed

    private void btnActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActiveActionPerformed
        setStatus(YearCommitteeStatus.ACTIVE);
    }//GEN-LAST:event_btnActiveActionPerformed

    private void btnRejectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRejectActionPerformed
        setStatus(YearCommitteeStatus.REJECT);
    }//GEN-LAST:event_btnRejectActionPerformed

    private void btnResignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResignActionPerformed
        setStatus(YearCommitteeStatus.RESIGN);
    }//GEN-LAST:event_btnResignActionPerformed

    private void btnSuspendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuspendActionPerformed
        setStatus(YearCommitteeStatus.SUSPEND);
    }//GEN-LAST:event_btnSuspendActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActive;
    private javax.swing.JButton btnReject;
    private javax.swing.JButton btnResign;
    private javax.swing.JButton btnSuspend;
    private javax.swing.JComboBox cboPost;
    private javax.swing.JComboBox cboYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAdded;
    private javax.swing.JTable tblRightTable;
    private javax.swing.JTextField txtNoOfMembers;
    // End of variables declaration//GEN-END:variables
    private List leftList;
    private List rightList;
    private List memberList;

    @Override
    public List<Component> getIdentityComponent() {
        return Arrays.asList(
                (Component) cboPost,
                (Component) cboYear,
                (Component) txtNoOfMembers,
                (Component) tblRightTable
        );
    }

    @Override
    public List<Component> getEsentialComponent() {
        return Arrays.asList();
    }

    @Override
    public List<Component> getOtherComponent() {
        return Arrays.asList(
                (Component) txtNoOfMembers
        );
    }

    @Override
    public Object initObject() {
        return new ArrayList<>();
    }

    @Override
    public Object[] initObjectList() {
        int row = tblRightTable.getRowCount();
        if (row != 0) {
            Object[] yearCommittees = new Object[]{};
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < row; i++) {
                if (!tblRightTable.getValueAt(i, 2).equals(((Member) rightList.get(i)).getStatus())) {
                    YearCommittee yearCommittee = new YearCommittee();
                    Member member;
                    member = new Member();
                    yearCommittee.setYear((Year) cboYear.getSelectedItem());
                    yearCommittee.setCommittee((Committee) cboPost.getSelectedItem());
                    yearCommittee.setNoOfMembers(Integer.parseInt(txtNoOfMembers.getText()));
                    member = ((Member) tblRightTable.getValueAt(i, 0));
                    member.setCommittee(((Committee) cboPost.getSelectedItem()).getCode());
                    yearCommittee.setMember(member);
                    yearCommittee.setStatus(tblRightTable.getValueAt(i, 2).toString());
                    arrayList.add(yearCommittee);
                }
            }
                yearCommittees = arrayList.toArray();
                return yearCommittees;
        } else {
            return null;
        }
    }

    @Override
    public void getUnbindObject(Object object) {
    }

    @Override
    public void bindComponents(ArrayList<Object> list, int index) {
    }

    @Override
    public List<Component> componentList() {
        return Arrays.asList(
                (Component) cboYear,
                (Component) cboPost,
                (Component) txtNoOfMembers,
                (Component) tblRightTable
        );
    }

}
